//
//  VersionInfoPopViewController.swift
//  ManBetX
//
//  Created by Cocoa on 2020/4/29.
//  Copyright © 2020 Fullen. All rights reserved.
//

import UIKit

/**
* 版本更新信息 - 弹框
*/

// 立即更新点击回调
typealias VersionInfoButtonClickBlock = () -> Void

class VersionInfoPopViewController: BasePopViewController,UITableViewDelegate,UITableViewDataSource{
    
    // IBOutlet
    @IBOutlet weak var bottomView: UIView!// 底层View
    @IBOutlet weak var container: UIView!// 信息界面
    // 信息界面底部距离底部间距
    @IBOutlet weak var containerBottomConstraint: NSLayoutConstraint!
    @IBOutlet weak var closeButton: UIButton!// 关闭按钮
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var infoLabe: UILabel!
    
    
    
    // Property
    var closeButtonClickBlock: VersionInfoButtonClickBlock?
    var dataArray: NSArray = NSArray.init()
    var infoString: String = ""
    
    /*
      测试：
     
    
     */
            
    
    // MARK:--便捷展示
    class func showVersionInfoPopViewController(
        infoString: String,
        dataArray: NSArray,
        closeButtonClickBlock: @escaping VersionInfoButtonClickBlock) {
        
        let popVC = VersionInfoPopViewController.instanceFromStoryboard()!
        popVC.closeButtonClickBlock = closeButtonClickBlock
        popVC.dataArray = dataArray
        popVC.infoString = infoString
        
        popVC.showHightLevel()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.layoutIfNeeded()
        self.view.backgroundColor = UIColor.clear
        self.infoLabe.text = self.infoString
        configureBottomView()
        self.tableView .reloadData()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        self.configureContainerHidden(hidden: false)
    }
    
    // 控制视图展示与消失
    func configureContainerHidden(hidden:Bool) {
        if hidden == true {
            UIView.animate(withDuration: 0.2, animations: {
                self.containerBottomConstraint.constant = -self.container.bounds.size.height
                self.view.layoutIfNeeded()
            }) { (finished) in
                self.dismiss()
            }
        }else {
            UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.5, options: .curveEaseInOut
                , animations: {
//                    let bottomConstant = self.container.bounds.size.height + 113.0
                    let bottomConstant = (self.view.bounds.size.height/2 - self.container.bounds.size.height/2)
                    self.containerBottomConstraint.constant = bottomConstant
                    self.view.layoutIfNeeded()
            }, completion: nil)
        }
    }
    
    // 背景图点击关闭
    func configureBottomView() {
        self.bottomView.whenTapped {[weak self] (gestureRecognizer) in
            
            self?.configureContainerHidden(hidden: true)
            if self?.closeButtonClickBlock != nil {
                self?.closeButtonClickBlock!()
            }
        }
    }
    
    // 按钮点击关闭
    @IBAction func closeButtonClick(_ sender: UIButton) {
        
        self.configureContainerHidden(hidden: true)
        
        if self.closeButtonClickBlock != nil {
            self.closeButtonClickBlock!()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 32.0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let infoString = self.dataArray.object(at: indexPath.row)
        
        let cell = tableView.dequeueReusableCell(withIdentifier: VersionInfoPopCell.getIdentifier(), for: indexPath) as! VersionInfoPopCell
        
        cell.configure(infoString as Any)
        
        return cell
    }
    
    
    
}
